package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.service.TimetableService;
import com.hushao.lims.weblims.util.ExcelUtil;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 930588706
 * @date 18/2/3
 * 课程
 */
@Controller
@RequestMapping("/timetable")
public class TimetableController {

    @Autowired
    private TimetableService timetableService;
    @PreAuthorize("hasAnyAuthority('ROLE_主管','ROLE_管理员','ROLE_超级管理员')")

    @GetMapping
    public String listClassByTeacherNameAndTerm() throws Exception{

        return "timetables/list";
    }

    /**
     * 课程导入
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程导入服务")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @PostMapping("/upload")
    @ResponseBody
    public ResponseData uploadFile(MultipartFile file, String termStartTime) throws Exception{
        Workbook wb=null;
        if (ExcelUtil.isExcel2007(file.getOriginalFilename())) {
            wb = new XSSFWorkbook(file.getInputStream());
        } else {
            wb = new HSSFWorkbook(file.getInputStream());
        }
        timetableService.uploadFile(wb.getSheetAt(0),termStartTime);
        wb.close();
        return new ResponseData(true,"课程信息导入成功!");
    }
}
